package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.support.v4.util.SimpleArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.ClazzContact;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.config.AppConfig;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.activity.contact.ContactDetailActivity;
import com.linkage.educloud.ah.view.activity.contact.ContactDetailPersonalActivity;
import com.zzhou.common.library.util.LogUtils;
import com.zzhou.common.library.util.StringUtil;
import com.zzhou.common.library.util.SystemTool;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 周振 on 2017/6/28.
 */

public class ContactsAdapter extends BaseAdapter<BaseAdapter.BaseViewHolder> {

    private List<ClazzContact> contacts;
    private ArrayMap<String, Integer> letters;

    private int simType = 0;
    private int shortdn = -1;
    private ArrayList<Long> ids;

    public ContactsAdapter(Context context) {
        super(context);
        this.contacts = new ArrayList<>();
        this.letters = new ArrayMap<>();
        simType = SystemTool.getPhoneInfos(mContext);
        ids = new ArrayList<>();
//        shortdn = BasicApplication._Context.geAppConfig().getInt(ShareWarehouse.HAS_SHORTDN, -1);
        decliShortClazzs();
    }
    public void updateDatas(List<ClazzContact> contacts, ArrayMap<String, Integer> letters){
        this.contacts.clear();
        this.letters.clear();
        this.contacts.addAll(contacts);
        this.letters.putAll((SimpleArrayMap<? extends String, ? extends Integer>) letters);
        notifyDataSetChanged();
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LogUtils.e("onCreateViewHolder = type "+ viewType);
        switch (viewType) {
            case 0:
                return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_contacts, null), true);
            case 1:
            default:
                return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_contacts, null), false);
        }

    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }

    @Override
    public int getItemViewType(int position) {
        ClazzContact contact = contacts.get(position);
        if (contact != null && contact.getType() == ClazzContact.ContactType.TEACHER) {//教师不显示字母
            return 1;
        }
        if (letters.get(contact.getFirstName()) == position) {//首位显示字母
            return 0;
        }
        return 1;
    }

    public class ViewHolder extends BaseViewHolder implements View.OnClickListener {

        private TextView letter;
        private CircleImageView avatar;
        private TextView name;
        private ImageView roleIcon;
        private ImageView phoneBtn;
        private ImageView vPhoneBtn;

        public ViewHolder(View itemView, boolean showLetter) {
            super(itemView);
            itemView.setOnClickListener(this);
            letter = (TextView) itemView.findViewById(R.id.letter_title);
            avatar = (CircleImageView) itemView.findViewById(R.id.avatar_iv);
            name = (TextView) itemView.findViewById(R.id.column_title);
            roleIcon = (ImageView) itemView.findViewById(R.id.t_img);
            phoneBtn = (ImageView) itemView.findViewById(R.id.ic_tel);
            vPhoneBtn = (ImageView) itemView.findViewById(R.id.ic_telv);
            if (showLetter) {
                letter.setVisibility(View.VISIBLE);
            } else {
                letter.setVisibility(View.GONE);
            }

        }

        @Override
        public void onClick(View v) {
            ClazzContact contact = contacts.get(getAdapterPosition()-1);
            if (contact.getId() == AccountConfig.getLoginAccount().getUserId()) {
                ContactDetailActivity.luncher(mContext,contact);
            }else{
                ContactDetailPersonalActivity.luncher(mContext,contact);
            }
        }

        @Override
        public void setData(int position) {
            LogUtils.e("#################### getAdapterPosition() = "+getAdapterPosition());
            ClazzContact contact = contacts.get(position);
            letter.setText(contact.firstName);
            name.setText(contact.getName());
            if ((1 == contact.invite || 0 == contact.invite)) {// 0,1表示 未登录过客户端或？？？
                avatar.setImageResource(R.mipmap.ic_default_avatar1);
            } else {
                ImageLoader.loadAvatar(mContext, contact.avatar_url, avatar);
            }
            if (contact.type == ClazzContact.ContactType.TEACHER) {
                roleIcon.setVisibility(View.VISIBLE);
            } else {
                roleIcon.setVisibility(View.GONE);
            }
            if (SystemTool.hasSimCard(mContext)) {
            /*只有1，手机有卡，手机卡为移动号,2，用户本身在这个班级下面开通班级网， 3，同班级下联系人班级网码不为空时。显示班级网图标*/
                if (simType == SystemTool.YIDONG && shortdn == Constants.VClazzType.Vclass
                        && ids.contains(contact.classid) && StringUtil.isNullOrEmpty(contact.short_dn)) {
                    phoneBtn.setVisibility(View.GONE);
                    vPhoneBtn.setVisibility(View.VISIBLE);
                } else {
                    vPhoneBtn.setVisibility(View.GONE);
                    if (AccountConfig.getLoginAccount().getUserId() == contact.id || StringUtil.isNullOrEmpty(contact.dn)) {
                        phoneBtn.setVisibility(View.GONE);
                    }else{
                        if (AccountConfig.getLoginAccount().getUserType() == Constants.UserType.PARENT) {
                            if (contact.isopen_phone == 1) {//1表示该联系人公开手机号
                                phoneBtn.setVisibility(View.VISIBLE);
                            }else{
                                phoneBtn.setVisibility(View.GONE);
                            }
                        }else{
                            phoneBtn.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }else{//如果没插卡，电话图标都不显示
                phoneBtn.setVisibility(View.GONE);
                vPhoneBtn.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 获取用户开通班级网的班级id集合
     */
    private void decliShortClazzs() {
        String clazzids = AppConfig.getInstance().getString(AppConfig.ShareKey.KEY_SHORTDN_CLAZZS, "");
        if (!StringUtil.isNullOrEmpty(clazzids)) {
            String[] clazzss = clazzids.split(",");
            for (int i = 0; i < clazzss.length; i++) {
                if (!StringUtil.isNullOrEmpty(clazzss[i]
                        .trim())) {
                    ids.add(Long
                            .parseLong(clazzss[i]));
                }

            }
        }
    }
}
